package com.mark.media.service.impl;

import com.mark.media.service.UserLoginService;
import com.mark.model.common.dtos.ResponseResult;
import com.mark.model.common.enums.AppHttpCodeEnum;
import com.mark.model.mappers.wemedia.WmUserMapper;
import com.mark.model.media.pojos.WmUser;
import com.mark.utils.jwt.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description :  java类作用描述
 * @Author :  Markburt
 * @CreateDate :  2020/3/8$ 下午 01:34$
 * @UpdateUser :  Markburt
 * @UpdateDate :  2020/3/8$ 下午 01:34$
 * @UpdateRemark :  Project Build
 * @Version :  1.0
 */

@Service

@SuppressWarnings("all")
public class UserLoginServiceImpl implements UserLoginService {


    @Autowired
    private WmUserMapper wmUserMapper;


    /***
     * 登录方法
     * @param user
     * @return
     */
    @Override
    public ResponseResult login(WmUser user) {
        if (StringUtils.isEmpty(user.getName()) && StringUtils.isEmpty(user.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "用户名或密码不能为空");
        }
        WmUser wmUser = wmUserMapper.selectByName(user.getName());
        if (wmUser != null) {
            if (user.getPassword().equals(wmUser.getPassword())) {
                Map<String, Object> result = new HashMap<>();
                wmUser.setPassword("");
                wmUser.setSalt("");
                result.put("token", AppJwtUtil.getToken(wmUser));
                result.put("user", wmUser);
                return ResponseResult.okResult(result);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");

        }
    }
}
